#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include "Breakthrough.h"
#include "MiniBreak.h"
using namespace std;

void listGames(vector<IGame*> games)
{
	cout << "Available games:\n";
	for (unsigned int i = 0; i < games.size(); ++i)
	{
		cout << i+1 << " - " << games[i]->getName() << '\n';
	}
	cout << '\n';
}

void checkInput(string input)
{
	/*string word;
	stringstream ss(input);
	while (ss >> word)
	{
		if 
	}*/
}

int main()
{
	Breakthrough breakthr;
	MiniBreak minibreak;
	string input;
	vector<IGame*> games;
	games.push_back( &breakthr );
	games.push_back( &minibreak );
	int currentGame = 0;
	

	cout << "Welcome to MAXIMUM Awesome!\n>";
	while (getline(cin, input))
	{
		if (input == "quit") 
		{ 
			break; 
		}
		else if (input == "start")
		{
			cout << "Starting game: " << currentGame+1 << endl; 
			games[currentGame]->start();
			cout << games[currentGame]->getName() << " Started"<< endl;
		}
		else if (input == "list")
		{
			listGames(games);
		}
		else if (input == "evaluate")
		{
			games[currentGame]->evaluate();
		}
		else if (input == "legal")
		{
			games[currentGame]->legal();
		}
		else if (input == "display")
		{
			games[currentGame]->display();
			cout << "Displaying game: " << currentGame+1 << endl;
		}
		else if (input.substr(0, 4) == "make")
		{
			string word;
			int dest[4];
			int i = 0;
			stringstream ss(input);
			while (ss >> word && i < 4) 
			{
				if (word != "make")
				{
					if (isdigit(word[0]))
					{
						dest[i] = atoi(word.c_str());
						++i;
					}
				}
			}

			if (sizeof(dest)/sizeof(int) == 4)
			{
				games[currentGame]->make(dest[0], dest[1], dest[2], dest[3]);
			}
			else
			{
				cout << sizeof(dest) << '\n';
				cout << "Make usage example: make 0 1 0 2\n";
			}
		}
		else if (input.substr(0, 4) == "game")
		{
			string word;
			stringstream ss(input);
			while (ss >> word)
			{
				if(isdigit(word[0]))
				{
					currentGame = atoi(word.c_str())-1;
					if (currentGame > games.size() || currentGame < 0)
					{
						cout << "Game number " << currentGame << " is not in the list.\n"
							 << "Type 'list' to see what games are available.\n";
						currentGame = 0;
					}
					break;
				}
			}
		}
		else if (input.substr(0, 5) == "level")
		{
			string level;
			stringstream ss(input);
			while (ss >> level)
			{
				if (level != "level")
				{
					break;
				}
			}
			games[currentGame]->level(level);
		}
		else if (input == "go")
		{
			games[currentGame]->go();
		}
		else if (input == "retract")
		{
			games[currentGame]->retract();
		}
		cout << '>';

	}

	return 0;
}
